package com.layer.cms.generate.util;

import lombok.extern.slf4j.Slf4j;

import java.sql.*;

/**
 * @author 小新
 * @description: 数据库链接帮助类
 * @datetime: 2024年4月28日 下午8:47:31
 */
@Slf4j
public class DBUtils {

    public static String dataBaseName;
    public static String username;
    public static String password;

    private static Connection connection;

    public static Connection getConnection() {

        if (null == connection) {

            String url = "jdbc:mysql://10.10.10.20:3306/" + dataBaseName + "?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai";
            try {

                Class.forName("com.mysql.cj.jdbc.Driver");
                connection = DriverManager.getConnection(url, username, password);
            } catch (ClassNotFoundException | SQLException e) {
                log.error("数据库链接失败...{}", e.getMessage());
            }
        }
        return connection;
    }

    public static void colse(ResultSet rs, PreparedStatement ps, Connection connection) {

        try {

            if (null != rs) {
                rs.close();
            }
            if (null != ps) {
                ps.close();
            }
            if (null != connection) {
                connection.close();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
